<?php
namespace Admin\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Paginatior\Paginator;
use Zend\Paginator\Adapter\Null as PageNull;
use Zend\Session\Container;
use Admin\Model\User;
use Admin\Model\Video;
use Admin\Model\Categories;

class IndexController extends AbstractActionController
{
	public function checkadmin()
	{	
		$session_user = new Container('user');
	    if($session_user->userid ==null && $session_user->userlever == null)
	    {
	    	return $this->redirect()->toRoute('admin',array('action' => 'login'));
		}
	}
    

	public function IndexAction()
	{
        $this->checkadmin();
        $this->layout('layout/admin');
		
        return new ViewModel();
	}

    public function regAction()
    {
        $res =$this->getUserTable()->saveUser();
        var_dump($res);die;
        return new ViewModel();
    }
   




	public function loginAction()
	{
		$viewModel = new ViewModel();
        $viewModel->setTerminal(true);
        
		$session_user = new Container('user');
        $session_user->username = null;
        $session_user->userid = null;
        $session_user->userlever = null;
         $request = $this->getRequest();
         if ($request->isPost()) {
         	//kiem tra gia tri form gui len va khooi tao user
            $data =$request->getPost();
             
             $result =$this->getUserTable()->treckLogin($data['user_name'],$data['password']);

             if($result == 0)
             {
                return $this->redirect()->toRoute('admin',array('action' => 'index'));
             	//return $this->redirect()->toUrl('http://localhost/GIAGOC/public/user/index');
                $this->layout()->message = "Đăng nhập thành công";
             }else
              if($result == 1)
             {
                $this->layout()->message= "Tên đăng nhập không chính xác";
             }else
             if($result == 2)
             {
                $this->layout()->message = "Mật khẩu không chính xác";
             }
         }
         return $viewModel;
        
	}


    public function logoutAction()
    {
        $session_user = new Container('user');
        $session_user->username=null;
        $session_user->userid=null;
        $session_user->userlever=null;
        $session_user->time=0;
        return $this->redirect()->toRoute('admin',array('action' => 'login'));
    }


   

	public function videoaddAction()
	{
		$this->checkadmin();
		$this->layout('layout/admin');
        $resultCate = $this->getCategoriesTable()->getAll();

        $request = $this->getRequest();
        if ($request->isPost()) {
            $data = array();
            $data = $request->getPost();
            
            if (isset($_FILES['image'])) {
                $resultImage = $this->getVideoTable()->Upfile($_FILES['image']);

                if ($resultImage !=null) {
                    $data['image'] = $resultImage;

                    $video = new Video();
                    $video->exchangeArray($data);
                    
                    $resultVideo = $this->getVideoTable()->saveVideo($video);
                    if ($resultVideo == 1) {
                        $this->layout()->message = "Succesfuly!!";
                    }
                   
                }
            }
        }
        return new ViewModel(array('cate'=>$resultCate));
	}

	
//*****
//**********************************************************************************************//
//*****
    public function indexvideoAction()
    {
        $this->checkadmin();
        $this->layout('layout/admin');
        $request = $this->getRequest();
        if($request->isPost())
        {
            $data = $request->getPost();
            if (isset($data['capnhat'])) {
                $video = new Video();
                $video->exchangeArray($data);
                $result = $this->getVideoTable()->saveVideo($video);
            }
        }

        if(isset($_GET['searchby']) && $_GET['searchby']==1)
        {
            $key =$_GET['search'];
            $paginator = $this->getVideoTable()->getvideobyid($key);
            $paginator->setCurrentPageNumber((int) $this->params()->fromQuery('page',1));
            $paginator->setItemCountPerPage(16);
            return new ViewModel (array('paginator' => $paginator));
        }
        else if(isset($_GET['searchby']) && $_GET['searchby']==2)
        {
            $key =$_GET['search'];
             $this->layout()->action = "indexvideo";
            $this->layout()->router = "admin";
            $paginator =$this->getVideoTable()->getbytitle($key);
            
            $paginator->setCurrentPageNumber((int) $this->params()->fromQuery('page',1));
            $paginator->setItemCountPerPage(16);
            return new ViewModel (array('paginator' => $paginator));
        }




        $paginator = $this->getVideoTable()->fetchAll();
        
        $this->layout()->action = "indexvideo";
        $this->layout()->router = "admin";
        $paginator->setCurrentPageNumber((int) $this->params()->fromQuery('page',1));
        $paginator->setItemCountPerPage(20);
        return new ViewModel (array('paginator' => $paginator));

    }
	

    public function editvideoAction()
    {
        $this->checkadmin();
       $this->layout('layout/admin');

       $id = (int) $this->params()->fromRoute('id',0);
       $resultVideo = $this->getVideoTable()->getVideo($id);
       $resultCate = $this->getCategoriesTable()->getAll();

       $request = $this->getRequest();
        if ($request->isPost()) {
            $data = array();
            $data = $request->getPost();
            
            if (isset($_FILES['image'])) {
                $resultImage = $this->getVideoTable()->Upfile($_FILES['image']);

                if ($resultImage !=null) {
                    $data['image'] = $resultImage;
               }
            }
                    $video = new Video();
                    $video->exchangeArray($data);
                    
                    $resultVideo = $this->getVideoTable()->saveVideo($video);
                    if ($resultVideo == 1) {
                        $this->layout()->message = "Succesfuly!!";
                        return $this->redirect()->toRoute('admin',array('action' => 'indexvideo'));
                    }
                    else
                    {
                         $this->layout()->message = "Update False!!";
                          return $this->redirect()->toRoute('admin',array('action' => 'editvideo','id'=>$id));
                    }
                   
                
        }
       return new viewModel(array('data'=>$resultVideo,'cate'=>$resultCate) );  
    }


    public function delvideoAction()
    {
        $this->checkadmin();
        $this->layout('layout/admin');
        $id = (int) $this->params()->fromRoute('id',0);
        $resultVideo = $this->getVideoTable()->getVideo($id);

        if($resultVideo !=null)
        {
            $request = $this->getRequest();
            if($request->isPost())
            {
                $data = $request->getPost();
                $result =$this->getVideoTable()->deleteVideo($resultVideo->video_id);
                if($result)
                {
                    $this->layout()->message ="Delete Success!";
                    return $this->redirect()->toRoute('admin',array('action' => 'indexvideo'));
                }
                else
                {
                    $this->layout()->message ="Delete false!";
                }
            }
        }

        return new ViewModel(array('data'=>$resultVideo));   
        

    }

//**********************************************************************************************************************//

    public function indexcateAction()
    {
        $this->checkadmin();
        $this->layout('layout/admin');

        if(isset($_GET['searchby']) && $_GET['searchby']==1)
        {
            $key =$_GET['search'];

            $paginator = $this->getCategoriesTable()->getcatebyid($key);
            $paginator->setCurrentPageNumber((int) $this->params()->fromQuery('page',1));
            $paginator->setItemCountPerPage(16);
            return new ViewModel (array('paginator' => $paginator));
        }
        else if(isset($_GET['searchby']) && $_GET['searchby']==2)
        {
            $key =$_GET['search'];
             $this->layout()->action = "indexcate";
            $this->layout()->router = "admin";
            $paginator =$this->getCategoriesTable()->getbyname($key);
            
            $paginator->setCurrentPageNumber((int) $this->params()->fromQuery('page',1));
            $paginator->setItemCountPerPage(16);
            return new ViewModel (array('paginator' => $paginator));
        }



        $paginator = $this->getCategoriesTable()->fetchAll();

        $this->layout()->action = "indexcate";
        $this->layout()->router = "admin";
        $paginator->setCurrentPageNumber((int) $this->params()->fromQuery('page',1));
        $paginator->setItemCountPerPage(20);
        return new ViewModel (array('paginator' => $paginator));
    }

    public function addcateAction()
    {
        $this->checkadmin();
       $this->layout('layout/admin');
       $request = $this->getRequest();
       if($request->isPost())
       {
            $data = $request->getPost();
            $categories = new Categories();
            $categories->exchangeArray($data);
            $resultCate =  $this->getCategoriesTable()->saveCategories($categories);
            if($resultCate==1)
            {
                $this->layout()->message = "Succesfuly!";
                 return $this->redirect()->toRoute('admin',array('action' => 'indexcate'));
            }
            else
            {
                $this->layout()->message = "Insert False!";
            }
       }


       return new ViewModel();
    }


    public function editcateAction()
    {
        $this->checkadmin();
        $this->layout('layout/admin');
        $id = (int) $this->params()->fromRoute('id',0);
        if (isset($id)) 
        {
            $resultCate = $this->getCategoriesTable()->getCategories($id);
        }

        $request = $this->getRequest();
        if ($request->isPost()) 
        {
            $data = $request->getPost();
            $categories = new Categories();
            $categories->exchangeArray($data);
            $resultSet = $this->getCategoriesTable()->saveCategories($categories);
            if($resultSet==1)
            {
                return $this->redirect()->toRoute('admin',array('action' => 'indexcate'));
            }
            else
            {
                $this->layout()->message ="Update false!";
            }
        }
        return new viewModel(array('data'=>$resultCate));
    }



    public function delcateAction()
    {
        $this->checkadmin();
        $this->layout('layout/admin');
        $id = (int) $this->params()->fromRoute('id',0);
        if (isset($id)) 
        {
            $resultCate = $this->getCategoriesTable()->getCategories($id);

            $request = $this->getRequest();
            if($request->isPost())
            {
                $resultSet = $this->getCategoriesTable()->deleteCate($id);
                var_dump($resultSet);
            }
        }
        return new ViewModel(array('data'=>$resultCate));
    }

	
//*****
//**********************************************************************************************//
//*****
    protected $VideoTable;
	public function getVideoTable()
     {
         if (!$this->VideoTable) {
             $sm = $this->getServiceLocator();
             $this->VideoTable = $sm->get('Admin\Model\VideoTable');
         }
         return $this->VideoTable;
     }

//*****
//**********************************************************************************************//
//*****
    protected $UserTable;
	public function getUserTable()
     {
         if (!$this->UserTable) {
             $sm = $this->getServiceLocator();
             $this->UserTable = $sm->get('Admin\Model\UserTable');
         }
         return $this->UserTable;
     }


     protected $CategoriesTable;
    public function getCategoriesTable()
     {
         if (!$this->CategoriesTable) {
             $sm = $this->getServiceLocator();
             $this->CategoriesTable = $sm->get('Admin\Model\CategoriesTable');
         }
         return $this->CategoriesTable;
     }
     //*****
//**********************************************************************************************//
//*****

}